package com.gress.shop.dao.impl;

import com.gress.shop.bo.Role;
import com.gress.shop.bo.example.RoleExample;
import com.gress.shop.dao.IRoleDao;
import com.gress.shop.mapper.RoleMapper;
import com.gress.shop.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created xiongwen with 2018/4/12 10:14
 * Description:
 */
@Service
public class RoleDaoImpl implements IRoleDao {

    @Resource
    private RoleMapper roleMapper;


    @Override
    public int countByExample(RoleExample example) {
        return roleMapper.countByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Role record) {
        String uuid = UUIDUtil.getUUID();
        record.setId(uuid);
        return roleMapper.insert(record);
    }

    @Override
    public String insertSelective(Role record) {
        String uuid = UUIDUtil.getUUID();
        record.setId(uuid);
        roleMapper.insertSelective(record);
        return uuid;
    }

    @Override
    public List<Role> selectByExample(RoleExample example) {
        return roleMapper.selectByExample(example);
    }

    @Override
    public List<Role> selectAll() {
        RoleExample example = new RoleExample();
        example.createCriteria().andIsValidEqualTo(1);
        return roleMapper.selectByExample(example);
    }

    @Override
    public Role selectByPrimaryKey(String id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Role record) {
        return roleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Role record) {
        return roleMapper.updateByPrimaryKey(record);
    }

    @Override
    public boolean checkIsExist(String roleId) {
        return null != selectByPrimaryKey(roleId);
    }

    @Override
    public Role queryByCode(String roleCode) {
        RoleExample example = new RoleExample();
        example.createCriteria().andRoleCodeEqualTo(roleCode);
        List<Role> roles = roleMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(roles)) {
            return null;
        }
        return roles.get(0);
    }
}
